/******************************************************************************
 * SVN­Version­Information:
 * ­­­­­­­­­­­­­­­­­­­­­­­­---------------------
 * $LastChangedRevision: 6 $
 * $LastChangedBy: c.a.duevel $
 * $LastChangedDate: 2009-08-26 15:12:37 +0000 (Wed, 26 Aug 2009) $
 * $HeadURL: http://robofight.googlecode.com/svn/trunk/Server/src/de/hft_stuttgart/botwar/server/model/RobotMode.java $
 *******************************************************************************/
package de.hft_stuttgart.botwar.server.model;

import de.hft_stuttgart.botwar.common.models.Position;
import java.util.Collection;

/**
 * On each turn the mode is asked by its robot
 * first where he should go to and then where he
 * should fire at.
 * @author c.a.duevel@gmail.com
 */
abstract class RobotMode {

    /**
     * @param thisRobot
     * @param robots All robots in the game.
     * @return The target this robot has to go to in order to act after this
     * mode.
     */
    abstract Position getTarget(Robot thisRobot, Collection<Robot> robots);

    /**
     * @param thisRobot
     * @param robots All robots in the game.
     * @return The robot to fire at after this mode.
     */
    abstract Robot fireAt(Robot thisRobot, Collection<Robot> robots);

    Robot findNearestVictim(final Robot thisRobot,
                            final Collection<Robot> robots) {
        double nearestDist = Double.MAX_VALUE;
        Robot robot = null;
        for (Robot r : robots) {
            if (r.isEnemy(thisRobot) && thisRobot.distanceTo(r) < nearestDist) {
                nearestDist = thisRobot.getPos().distance(r.getPos());
                robot = r;
            }
        }
        if(robot==null){
            throw new RuntimeException("Searching for enemy robot with no" +
                    " enemies left");
        }
        return robot;
    }
}
